//
//  CZStatusRetweetView.swift
//  WHWeiBo01
//
//  Created by whitcast on 16/7/13.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SnapKit


//  转发微博视图
class CZStatusRetweetView: UIView {
    
    //  记录转发微博底部约束
    var retweetViewBottom: Constraint?
    //  持有的viewModel
    var statusViewModel: CZStatusViewModel? {
        didSet {
            //  设置转发微博内容
            //contentLabel.text = statusViewModel?.retweetContent
            contentLabel.attributedText = statusViewModel?.retweetAttributedStr
            //  卸载上一次底部约束
            retweetViewBottom?.uninstall()
            
            //  判断是否有转发微博配图
            if let count = statusViewModel?.status?.retweeted_status?.pic_urls?.count where count > 0 {
                //  有配图
                //  设置模型
                pictureView.picUrls = statusViewModel?.status?.retweeted_status?.pic_urls
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.retweetViewBottom = make.bottom.equalTo(pictureView).offset(HomeCellMargin).constraint
                })
                pictureView.hidden = false
                
            } else {
                //  没有配图
                pictureView.hidden = true
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.retweetViewBottom = make.bottom.equalTo(contentLabel).offset(HomeCellMargin).constraint
                })
            }
            
            
        }
    }

    // MARK: -- 懒加载控件
    private lazy var contentLabel: UILabel = {
        let label = UILabel(textColor: UIColor.grayColor(), fontSize: HomeFontSize)
        //  多行显示
        label.numberOfLines = 0
        return label
    }()
    //  转发微博配图
    private lazy var pictureView: CZStatusPictureView = {
    
        let collectionView = CZStatusPictureView()
        //  设置配图的背景色跟转发微博的背景色一致
        collectionView.backgroundColor = self.backgroundColor
        return collectionView
    }()
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //  添加视图设置约束
    private func setupUI() {
        contentLabel.backgroundColor = UIColor(white: 0.95, alpha: 1)
        //  添加控件
        addSubview(contentLabel)
        addSubview(pictureView)
        //  设置约束
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self).offset(HomeCellMargin)
            make.leading.equalTo(self).offset(HomeCellMargin)
            make.trailing.equalTo(self).offset(-HomeCellMargin)
        
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp_bottom).offset(HomeCellMargin)
        }
        
        //  关键的约束 
        //  转发微博视图的底部约束等于我们转发内容label的底部约束加上一个间距
        self.snp_makeConstraints { (make) -> Void in
            self.retweetViewBottom = make.bottom.equalTo(pictureView).offset(HomeCellMargin).constraint
        }
        
        
        
        
    
    }
    
   

}
